#pragma once
#include <cstdlib>
#include <log.h>
#include <string>
#include <sys/stat.h>
#include <fstream>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <random>
#include <sstream>
#include <ctime>
#include <iomanip>
#include <cstring>
namespace lyt{
    
    class FileUtil{
    public:
        static bool IsExist(const std::string& path_name){
            // st用来保存文件的元数据
            struct stat st;
            // stat用来获取指定路径的文件或目录的状态信息
            if(stat(path_name.c_str(),&st) == 0) return true;
            return false;
        }

        static std::string ReadText(const std::string& path){
            std::ifstream ifs(path);
            std::string ret;
            std::string temp;
            while(std::getline(ifs,temp)){
                ret += temp;
                ret += "\n";
            }
            ifs.close();
            return ret;
        }
        static bool WriteText(const std::string& path,const std::string& content){
            std::ofstream ofs(path);
            if (!ofs.is_open()) {
                debuglog << "[FileUtil::WriteText] 打开文件失败: " << path
                        << ", errno = " << errno << ", 错误信息: " << std::strerror(errno) << std::endl;
                return false;
            }
            ofs.write(content.data(), content.size());
            bool ret = !ofs.fail();
            ofs.close();
            return ret;
        }
    };
    class CompareFiles{
    public:
        static bool CompareFilesLineByLine(const std::string& file1, const std::string& file2) {
            std::ifstream f1(file1), f2(file2);
            if (!f1.is_open() || !f2.is_open()) {
                errorlog << "fail to open file";
                return false;
            }
    
            std::string line1, line2;
            int line_num = 1;
            while (true) {
                bool r1 = static_cast<bool>(std::getline(f1, line1));
                bool r2 = static_cast<bool>(std::getline(f2, line2));
                if (!r1 && !r2) {
                    // 两个都读完了
                    return true;
                }  
                if (r1 != r2 || line1 != line2) {
                    return false;
                }   
                ++line_num;
            }
        }
    };
    class Generate_UUID{
    public:
        static std::string generate_uuid(){
            boost::uuids::random_generator generator;;
            boost::uuids::uuid uuid = generator();
            return boost::uuids::to_string(uuid);
        }
    };
    class Generate_Token{
    public:
        static std::string generate_token(){
            std::stringstream ss;
            std::random_device rd;
            std::mt19937 gen(rd());
            std::uniform_int_distribution<> dis(0, 15);
            for (int i = 0; i < 32; ++i)
                ss << std::hex << dis(gen);
            return ss.str();
        }
    };
    class GetTimestamp{
    public:
        static long long gettimestamp(){
            // 获取当前时间点
            auto now = std::chrono::system_clock::now();

            // 转换为自纪元以来的毫秒数（时间戳）
            auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
            
            // 获取时间戳的整数值
            long long timestamp = duration.count();

            return timestamp;
        }
    };
    class GetTimeISO{
    public:
        static std::string gettimeiso(){
            std::time_t t = std::time(nullptr);
            std::tm local_tm = *std::localtime(&t);

            std::ostringstream oss;
            oss << std::put_time(&local_tm, "%Y-%m-%d %H:%M:%S");
            return oss.str();
        }
        static std::string changetoios(const long long& timestamp){
                // 将毫秒时间戳转为 time_t（秒）
            std::time_t time_sec = timestamp / 1000;

            // 转换为本地时间结构
            std::tm tm_time;
            localtime_r(&time_sec, &tm_time);

            // 格式化为 YYYY-MM-DD HH:MM
            std::ostringstream oss;
            oss << std::put_time(&tm_time, "%Y-%m-%d %H:%M");

            return oss.str();  // 例如：2025-07-17 21:44
        }
    };
    class CharacterExtraction{
    public:
        static std::string charextrac(const std::string& str,const int length){
            if(str.size() <= length) return str;
            else{
                return str.substr(0,length) + "...";
            }
        }
    };
    class RandomValue{
    public:
        static float random(float min, float max){
            srand(time(nullptr));
            int num = (rand() + min) * 100;

            return  (float)(num % (int)(max * 100)) / 100;
        }
        static int random(int min, int max){
            srand(time(nullptr));
            return (rand() + min) % max;
        }
    };
}